package com.example.sms.config;

import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Bus {

    private final Map<Class<?>, Object> handlerMap = new HashMap<>();
    private final Map<Class<?>, Method> methodMap = new HashMap<>();

    public void register(Class<?> argumentType, Object target, Method method) {
        handlerMap.put(argumentType, target);
        methodMap.put(argumentType, method);
    }

    public Object post(Object argument) {
        Class<?> argumentType = argument.getClass();
        Object target = handlerMap.get(argumentType);
        Method method = methodMap.get(argumentType);

        return ReflectionUtils.invokeMethod(method, target, argument);
    }

}
